package com.jy.api.service.impl;

import com.jy.api.dao.StockCheckOrderDao;
import com.jy.api.domain.StockCheck;
import com.jy.api.exception.MyException;
import com.jy.api.service.StockCheckOrderService;
import com.jy.api.service.StockCheckService;
import com.jy.api.signUpReq.stock.StockCheckSignUpReq;
import com.jy.api.util.InventoryUrlUtil;
import com.jy.api.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

/**
 * Created by lihu on 2018/2/2.
 */
@Service
public class StockCheckOrderServiceImpl implements StockCheckOrderService {


    @Autowired
    InventoryUrlUtil urlUtil;

    @Autowired
    RestTemplate restTemplate;


    @Autowired
    StockCheckOrderDao stockCheckOrderDao;

    @Autowired
    StockCheckService stockCheckService;

    @Override
    @Transactional
    public void StockCheckOrder(StockCheckSignUpReq signupReq) {
        switch (signupReq.getSource()) {
            /**
             *    柜台判定
             */
            case COUNTER:
                counter(signupReq);
                break;
            /**
             * 仓库盘点
             */
            case WAREHOUSE:
                warehouse(signupReq);
                break;
        }


    }

    /**
     * 柜台盘点相关数据处理
     *
     * @param signupReq
     */
    private void counter(StockCheckSignUpReq signupReq) {
        /**
         * 添加盘库记录
         */
        //StockCheck order = new StockCheck(signupReq);
        StockCheck order = new StockCheck();
        stockCheckOrderDao.save(order);
        stockCheckService.StockCheck(signupReq);

    }

    /**
     * 仓库盘点相关数据处理
     *
     * @param signupReq
     */
    private void warehouse(StockCheckSignUpReq signupReq) {
        Result result = restTemplate.postForEntity(urlUtil.getStockCheck(), signupReq, Result.class).getBody();
        if (result.getCode() != 200) {
            throw new MyException("系统错误");
        }
    }
}
